package com.example.demo.zhoushengwen.utils;

import org.apache.commons.io.input.CloseShieldInputStream;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@Component
public class RequestUtil
{
    public String getRequestBody(HttpServletRequest request)
    {
        String body = "";
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try
        {
            //使用Apache的CloseShieldInputStream对request中的输入流封装,防止流内部关闭
            inputStream = new CloseShieldInputStream(request.getInputStream());
            if (inputStream != null)
            {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) != -1)
                {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            }
            else
            {
                stringBuilder.append("");
            }
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if (bufferedReader != null)
            {
                try
                {
                    bufferedReader.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            
            if (inputStream != null)
            {
                try
                {
                    inputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return stringBuilder.toString();
    }
    
    /**
     * 获取客户端ip地址
     * @param request
     * @return
     */
    public static String getCliectIp(HttpServletRequest request)
    {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }
        
        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ip.split(",");
        for (final String str : arr)
        {
            if (!"unknown".equalsIgnoreCase(str))
            {
                ip = str;
                break;
            }
        }
        return ip;
    }
    
    /**
     * 对象深度克隆
     * @param obj
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    private <T extends Serializable> T clone(T obj)
    {
        T cloneObj = null;
        try
        {
            //写入字节流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream obs = new ObjectOutputStream(out);
            obs.writeObject(obj);
            obs.close();
            
            //分配内存，写入原始对象，生成新对象
            ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(ios);
            //返回生成的新对象
            cloneObj = (T)ois.readObject();
            ois.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return cloneObj;
    }
}